package com.project.thisDatabase.redis;

import com.project.thisCommon.common.TimeoutMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.event.EventListener;
import org.springframework.data.redis.core.RedisKeyExpiredEvent;
import org.springframework.stereotype.Component;

@ConditionalOnProperty(name = "spring.data.redis.listener", havingValue = "true")
@Component
public class RedisListener {

    TimeoutMap timeout = new TimeoutMap();

    @EventListener(value = RedisKeyExpiredEvent.class)
    public void redisKeyExpired(RedisKeyExpiredEvent event) {
        if (null != event) {
            String source = new String(event.getSource());
            String key = new String(event.getId());
            if (timeout.containsPut(source, null, 5000)) {
                String.format(String.format("=================>redis event: %s; channel: %s;", event, event.getChannel()));
                RedisItemContent bean = RedisSetting.getBean(event.getKeyspace());
                bean.getRedisItem().callback(key, source);
            }
        }
    }

}
